<?php
$page_title = 'eTAT| Add Question';
$access_level = 1;
$script = 'survey';
require_once '../../controller.php';
require_once '../../classes/Survey.php';
require_once '../../classes/Question.php';

$q_survey = new Survey();
$q_question = new Question();
if(isset($_SESSION['logged_in'])) {
	$instructor_id = $_SESSION['logged_in'];
}

if(isset($_POST['add_new_question'])) {
	if(isset($_POST['required'])){
		$required='Y';
	}
	if(isset($_POST['section'])){
		$section='Y';
	}
	$sid = $_POST['sid'];
	$q_id = $_POST['q_id']; 
	$data = array(
	              'survey_id'=>$_POST['sid'],
	              'type'=>addslashes($_POST['type']),
	              'required'=>$required,
	              'question'=>addslashes($_POST['question_text']),
	              'create_section'=>$section,
	              'order'=>addslashes($_POST['order']),
	              'response1'=>addslashes($_POST['answer_choice_1']),
	              'response2'=>addslashes($_POST['answer_choice_2']),
	              'response3'=>addslashes($_POST['answer_choice_3']),
	              'response4'=>addslashes($_POST['answer_choice_4']),
	              'response5'=>addslashes($_POST['answer_choice_5']),
	              'response6'=>addslashes($_POST['answer_choice_6'])
	            );
	            
	if(!isset($_REQUEST['qid'])){
		$q_question->addQuestion($data);
		header("location: question.php?sid=$sid");
	}else{
		$id = $_REQUEST['qid'];
		$q_question->updateQuestion($id,$data);
		header("location: question.php?sid=$sid&qid=$id");
	}            
	
}

if(isset($_POST['delete_question'])){
	$id = $_POST['q_id'];
	$q_question->deleteQuestion($id);

}

if(isset($_POST['finish'])){
	$sid = $_POST['sid'];
	header("location: finish.php?sid=$sid");
}

if(isset($_POST['preview'])){
	$sid = $_POST['sid'];
	header("location: preview.php?sid=$sid&sec=1&num=0");
}

if(isset($_POST['set_conditions'])){
	$sid = $_POST['sid'];
	header("location: branching.php?sid=$sid");
}

if(!isset ($_REQUEST['sid'])){
	$sql = "SELECT MAX(`id`)FROM `survey` WHERE `instructor_id` = '$instructor_id'";
	$sid = mysql_query($sql)or die(mysql_error());
	$result = mysql_fetch_row($sid);

	foreach($result as $key=>$value){
		$survey_id = $value;
	}
    $num =0;

}else{
	$survey_id = $_REQUEST['sid'];
	$questions = $q_survey->getQuestions($survey_id);
	if($questions){
	foreach ($questions as $i => $values) {
		$question_id[$i] = $values->id;
		$type[$i] = $values->type;
		$required[$i] = $values->required;
		$question_value[$i] = $values->question	;
		$create_section[$i] = $values->create_section;
		$order[$i] = $values->order;
		$response1[$i] = $values->response1;
		$response2[$i] = $values->response2;
		$response3[$i] = $values->response3;
		$response4[$i] = $values->response4;
		$response5[$i] = $values->response5;
		$response6[$i] = $values->response6;
	}
	$num = sizeof($type);
	}
	
}
 
if(isset ($_POST['edit'])){
	$qid = $_POST['edit_question'];
	$question = $q_question->getQuestion($qid);
	 
	$q_type = $question->type;
	$q_required = $question->required;
	$q_question_value = $question->question;
	$q_create_section = $question->create_question;
	$q_order = $question->order;
	$q_response1 = $question->response1;
	$q_response2 = $question->response2;
	$q_response3 = $question->response3;
	$q_response4 = $question->response4;
	$q_response5 = $question->response5;
	$q_response6 = $question->response6;

}
require_once '../../header.php';
?>


<div id="question-create">
<table width="300px">
	<form name="question" method="post"
		action="<?php  if(isset($_POST['edit_question'])){echo 'question.php?qid='.$qid;}else{echo 'question.php';}?> ">
	<tr>
		<th> Type:</th>

		<td><select id='q-type' name="type">
			<option onclick='javascript:hideAnswers();javascript:showRequired();'
<?php if(isset($_POST['edit_question']) && $q_type =='Text')echo 'selected="selected"';?>>Text</option>
			<option onclick='javascript:hideAnswers();javascript:showRequired();'
<?php if(isset($_POST['edit_question']) && $q_type =='Yes/No')echo 'selected="selected"';?>>Yes/No</option>
			<option onclick='javascript:showAnswers();javascript:showRequired();'
<?php if(isset($_POST['edit_question']) && $q_type =='Radio Button')echo 'selected="selected"';?>>Radio
			Button</option>
			<option onclick='javascript:showAnswers();javascript:showRequired();'
<?php if(isset($_POST['edit_question']) && $q_type =='Check Box')echo 'selected="selected"';?>>Check
			Box</option>
			<option onclick='javascript:showAnswers();javascript:showRequired();'
<?php if(isset($_POST['edit_question']) && $q_type =='Rate')echo 'selected="selected"';?>>Rate</option>
			<option onclick='javascript:showAnswers();javascript:showRequired();'
<?php if(isset($_POST['edit_question']) && $q_type =='Drop Down')echo 'selected="selected"';?>>Drop
			Down</option>
			<option onclick='javascript:hideAnswers();javascript:showRequired();'
<?php if(isset($_POST['edit_question']) && $q_type =='Date')echo 'selected="selected"';?>>Date</option>
			<option onclick='javascript:hideAnswers();javascript:showRequired();'
<?php if(isset($_POST['edit_question']) && $q_type =='Numeric'){echo 'selected="selected"';}?>>Numeric</option>
			<option onclick='javascript:hideAnswers();javascript:showSection();'
<?php if(isset($_POST['edit_question']) && $q_type =='Section Text'){echo 'selected="selected"';}?>>Section
			Text</option>

		</select></td>

	</tr>

	<tr>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr>
		<th> Text:</th>
		<td><textarea rows="1" cols="30" name="question_text"><?php if(isset($_POST['edit_question'])){echo $q_question_value;}?></textarea></td>
	</tr>
	<tr>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr id ='required'>
		<th>Response Required:</th>
		<td><input type="checkbox" name="required" <?php if(isset($_POST['edit_question'])&& $q_required=='Y'){
			echo 'checked="checked"';
		}?>></td>
	</tr>
	<tr id ='create_section'>
		<th>Create Section:</th>
		<td><input type="checkbox" name="section" <?php if(isset($_POST['edit_question'])&& $q_create_section=='Y'){
			echo 'checked="checked"';
		}?>></td>
	</tr>
	<tr id='a1'>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr id='a2'>
		<th>Add answer options</th>
	</tr>
	<tr id='a3'>
		<th>Answer 1:</th>
		<td><input type="text" name=" answer_choice_1"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response1;}?>"></td>
	</tr>
	<tr id='a4'>
		<th>Answer 2:</th>
		<td><input type="text" name=" answer_choice_2"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response2;}?>"></td>
	</tr>
	<tr id='a5'>
		<th>Answer 3:</th>
		<td><input type="text" name=" answer_choice_3"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response3;}?>"></td>
	</tr>
	<tr id='a6'>
		<th>Answer 4:</th>
		<td><input type="text" name=" answer_choice_4"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response4;}?>"></td>
	</tr>
	<tr id='a7'>
		<th>Answer 5:</th>
		<td><input type="text" name=" answer_choice_5"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response5;}?>"></td>
	</tr>
	<tr id='a8'>
		<th>Answer 6:</th>
		<td><input type="text" name=" answer_choice_6"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response6;}?>"></td>
	</tr>
	<tr>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr>
		<td><input type="hidden" name="sid" value=" <?php echo $survey_id;?>"></td></tr>
	<tr>
		<td><input type="hidden" name="q_id" value=" <?php if(isset($_POST['edit_question'])){echo $qid;}else{echo 'new';}?>"></td></tr>	
	<tr>
		<td><input type="hidden" name="order" value=" <?php if(isset($_POST['edit_question'])){echo $q_order;}else { echo $num+1;}?>"></td></tr>	
		<tr>
		    <td><input type="submit" name="delete_question" value="Delete"></td>
			<td><input type="submit" name="add_new_question" value=" Add/Save Question"></td>
			<td><input type="submit" name="set_conditions" value ="Set Conditions"></td>
			<td><input type="submit" name="preview" value="Preview Survey"></td>
		    <td><input type="submit" name=finish value="End Survey"></td>
		</tr>
	
	</form>
</table>


</div>

<div id="question-display">
<h1><strong>Existing Questions</strong></h1>
<form name="display" method="post" 	action="<?php echo 'question.php?sid='.$survey_id;?>">
<select name="edit_question" style="width: 400px">
<?php 
    $question_num =1;
    for($i=0;$i<$num;$i++){
	if($create_section[$i]=='Y'){  ?>
	<option  disabled="disabled" value="<?php echo $question_id[$i]?>"><?php echo '<-------section break/ new section-------> ';?></option>

	<?php }
	if($type[$i]=='Section Text'){
		?>
	<option value="<?php echo $question_id[$i]?>"><?php echo '<strong>SECTION TEXT.</strong> '.$question_value[$i];?></option>
	;
	<?php }else{?>
	<option value="<?php echo $question_id[$i]?>"><?php echo $question_num.'. '.$question_value[$i];?></option>
	;
	<?php
	$question_num++;
	}
}
?>
</select> <br>
<br>
<input name="edit" type="submit" value="Modify Question"> 

</form>
</div>




<?php
require_once '../../footer.php';
?>